<?php
/**
 * 用户管理
 * @author xiaosong
 */
class user extends admin{
    public function __construct(){
        parent::__construct();
    }
    
    //用户列表
    public function index(){
        $school_id=(int)$_SESSION['client']['jx_id'];
        $where=' where user_type=1 and school_id='.$school_id;
        if(isset($_GET['k'])&&$_GET['k']!='') {
            $k=trim($_GET['k']);
            if(preg_match('/^1[3-9][0-9]{9}$/',$k)) $where.=' and mobile='.$k;
            else $where.=' and real_name like "%'.$k.'%"';
        }
        if(isset($_GET['kemu'])&&is_numeric($_GET['kemu'])) {
            $where.=' and kemu="'.(int)$_GET['kemu'].'"';
        }
        if(isset($_GET['taocan'])&&is_numeric($_GET['taocan'])) {
            $where.=' and taocan_id='.(int)$_GET['taocan'];
        }
        
        $total=db()->getOne('select count(*) from x_user '.$where);
        if(isset($_GET['pagesize'])&&(int)$_GET['pagesize']>0) $pagesize=(int)$_GET['pagesize'];
        else $pagesize=C('PAGE_SIZE');
        if (isset($_GET['p']) && (int) $_GET['p'] > 0) $p = (int) $_GET['p'];
        else $p = 1;
        $limit=' limit '.($p-1)*$pagesize.','.$pagesize;
        $list=db()->getAll('select id,coach_id,taocan_id,real_name,sex,mobile,kemu from x_user '.$where.' order by id DESC '.$limit);
        if($list){
            foreach ($list as $k=>$v){
                $coach_name=db()->getOne('select real_name from x_user where id='.$v['coach_id']);
                $list[$k]['coach']=$coach_name?$coach_name:'--';
                $taocan_title=db()->getOne('select title from x_taocan where id='.$v['taocan_id']);
                $list[$k]['taocan']=$taocan_title?$taocan_title:'--';
            }
        }
        if(!$list) $list=array();
        $page = array('total' => $total,'pagesize' =>$pagesize,'curpage' => $p);
        $this->ajaxReturn(array('status'=>1,'data'=>$list,'page'=>$page));
    }
    
    //检测用户是否已注册
    public function check_user(){
        $mobile=$_POST['m'];
        $exits=db()->getOne('select id from x_user where mobile='.$mobile.' limit 1');
        if($exits) $this->ajaxReturn(array('status'=>1,'info'=>'该手机号已在平台注册'));
        else $this->ajaxReturn(array('status'=>0,'info'=>'该手机号未注册'));
    }
	
     public function add(){
        $user['mobile']=trim($_POST['mobile']);
        $user_info=db()->getRow('select id,user_type from x_user where mobile='.$user['mobile']);
        if($user_info){
            if($user_info['user_type']!=0) $this->ajaxReturn(array('status'=>0,'info'=>'改学员已经报过名了'));
            else{
                $_POST['id']=$user_info['id'];
                $this->edit();
            }
        }
        $id_number=trim($_POST['id_number']);
        if(strlen($id_number)<18) $this->ajaxReturn(array('status'=>0,'info'=>'身份证号格式不正确'));
        $user=get_info_by_idnum($id_number);
        $user['regtime']=NOW;
        $user['id_number']=$id_number;
        $user['real_name']=trim($_POST['real_name']);
        $user['mobile']=trim($_POST['mobile']);
        $user['nickname']='用户 '.substr_replace($user['mobile'],'****',4,8);
        $user['avatar']='';
        $user['user_type']=0;
        $user['kemu']=(int)$_POST['kemu'];
        $insert_id=db()->insert('x_user',$user);
        if($insert_id>0){
            insertlogs('create','成功添加学员：'.$user['real_name'],$user,$insert_id);
            db()->insert('x_student_extend',array('student_id'=>$insert_id,'bm_time'=>NOW));
            if(isset($_POST['file'])&&count($_POST['file'])>0){
                $a=array(1=>'身份证正面',2=>'身份证背面');
                foreach($_POST['file'] as $k=>$v){
                    $type=explode('_',$k,2);//$type[0]文件格式后缀；$type[1]文件类型（1：身份证，3教练证）
                    if(!isset($type[1])||!is_numeric($type[1])) break;
                    else{
                        if(empty($v)) continue;//如果文件为空，跳过
                        $f['uid']=$insert_id;
                        $f['title']=$a[$type[1]];
                        $f['file']=$v;
                        $f['upload_time']=NOW;
                        $f['file_type']=$type[1];
                        $file_insert_id=db()->insert('x_user_file',$f);
                        insertlogs('create','新增学员('.$user['real_name'].')资料:'.$a[$type[1]],$f,$file_insert_id);
                    }
                }
            }
            $this->ajaxReturn(array('status'=>1,'uid'=>$insert_id));
        }
        else $this->ajaxReturn(array('status'=>0,'info'=>db()->getError()));  
    }
    
    public function baoming(){
        $uid=(int)$_POST['uid'];
        $user_info=db()->getRow('select real_name,mobile,id_number,money from x_user where id='.$uid);
        if(!$user_info)  $this->ajaxReturn(array('status'=>0,'info'=>'获取用户信息失败'));
        $data['uid']=$uid;
        $data['real_name']=$user_info['real_name'];
        $data['mobile']=$user_info['mobile'];
        $data['id_number']=$user_info['id_number'];
        $data['school_id']=$_SESSION['client']['jx_id'];
        $data['school_name']=db()->getOne('select name from x_jiaxiaoinfo where id='.$_SESSION['client']['jx_id']);
        $data['taocan_id']=(int)$_POST['taocan_id'];
        $data['taocan_name']=$_POST['taocan_name'];//套餐名称
        $data['taocan_price']=$_POST['taocan_price'];//套餐金额
        $data['has_pay']=$_POST['has_pay'];
        $data['create_time']=NOW;
        $data['remark']=trim($_POST['remark']);
        if($data['has_pay']>=$data['taocan_price']) $data['status']=100;//全款
        else $data['status']=1;//部分付款
        $insert_id=db()->insert('x_baoming',$data);//添加报名信息
        if($insert_id){
            db()->insert('x_jindu',array('uid'=>$uid,'create_time'=>NOW,'info'=>'线下报名'));
            db()->insert('x_jindu',array('uid'=>$uid,'create_time'=>NOW,'info'=>'线下缴费'));
            //添加支付订单
            insertlogs('create','报名成功：'.$data['real_name'],$data,$insert_id);
            $order['school_id']=$_SESSION['client']['jx_id'];
            $order['uid']=$uid;
            $order['bm_id']=$insert_id;
            $order['order_no']=date('ymdHis').mt_rand(100,999).substr($_SESSION['user']['id'],-1);//生成订单号
            $order['title']= $data['taocan_name'];
            $order['amount']=$data['has_pay'];
            $order['create_time']=$order['pay_time']=NOW;
            $order['pay_type']=(int)$_POST['pay_type'];
            $order['status']=1;
            db()->insert('x_order',$order);
            //添加用户资金记录
            $a['uid']=$uid;
            $a['deal_name']='充值';
            $a['deal_amount']=$data['has_pay'];
            $a['deal_type']=1;//1代表充值
            $a['change_type']=1;//1代表收入，2支出
            $a['user_balance']=$user_info['money']+$data['has_pay'];//收入
            $a['ip']=get_client_ip(0,true);
            $a['create_time']=NOW;
            db()->insert('x_user_amount_log',$a);//充值资金记录
            $a['deal_name']='报名费';
            $a['deal_type']=3;//3代表报名费
            $a['change_type']=2;//支出
            $a['user_balance']=$user_info['money'];//支出
            db()->insert('x_user_amount_log',$a);//支出资金记录
            //更新用户信息
            db()->update('x_user',array('user_type'=>1,'school_id'=>$_SESSION['client']['jx_id'],'taocan_id'=>$data['taocan_id']),'id='.$uid);
            $this->ajaxReturn(array('status'=>1));
        }else{
            $this->ajaxReturn(array('status'=>0,'info'=>'添加失败'));
        }
    }
    
    //编辑用户
    public function edit(){
        $id=(int)$_POST['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'缺少参数'));
        $user['mobile']=trim($_POST['mobile']);
        if(db()->getOne('select id from x_user where mobile='.$user['mobile'].' and id<>'.$id)) $this->ajaxReturn(array('status'=>0,'info'=>'手机号已经被注册'));
        $user['real_name']=trim($_POST['real_name']);
        $user['mobile']=trim($_POST['mobile']);
        $user['id_number']=trim($_POST['id_number']);
        $user['kemu']=(int)$_POST['kemu'];
        $affert_num=db()->update('x_user',$user,'id='.$id);
        if($affert_num!==false){
            if($affert_num>0) insertlogs('update','更新学员信息:'.$user['real_name'],$user,$id);
            if(isset($_POST['file'])&&count($_POST['file'])>0){
                $a=array(1=>'身份证正面',2=>'身份证背面');
                foreach($_POST['file'] as $k=>$v){
                    $type=explode('_',$k,2);//$type[0]文件格式后缀；$type[1]文件类型（1：身份证，3教练证）
                    if(!isset($type[1])||!is_numeric($type[1])) break;
                    $file=db()->getOne('select file from x_user_file where uid='.$id.' and file_type='.$type[1].' order by id desc limit 1');
                    if($file!==false){
                        if(empty($v)){
                            db()->delete('x_user_file','uid='.$id.' and file_type='.$type[1]);
                            insertlogs('delete','删除学员('.$user['real_name'].')资料:'.$a[$type[1]],array(),$id);
                        }
                        elseif($file==$v) continue;
                        else{
                            $f['upload_time']=NOW;
                            $f['file']=$v;
                            db()->update('x_user_file',$f,'uid='.$id.' and file_type='.$type[1]);
                            insertlogs('update','更新学员('.$user['real_name'].')资料:'.$a[$type[1]],$f,$id);
                        }
                    }else{
                        $f['uid']=$id;
                        $f['title']=$a[$type[1]];
                        $f['file']=$v;
                        $f['upload_time']=NOW;
                        $f['file_type']=$type[1];
                        $insert_id=db()->insert('x_user_file',$f);
                        insertlogs('create','添加学员('.$user['real_name'].')资料:'.$a[$type[1]],$f,$insert_id);
                    }
                    
                }
            }
            $this->ajaxReturn(array('status'=>1,'uid'=>$id));
        }
        else $this->ajaxReturn(array('status'=>0,'info'=>$affert_num));
    }
    //学员详细信息
    public function info(){
        $id=(int)$_GET['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'参数id错误'));
        $info=db()->getRow('select id,coach_id,real_name,id_number,mobile,sex,money,frozen_money,kemu,status,regtime,lasttime from x_user where id='.$id);
        if($info){
            $coach=db()->getOne('select real_name from x_user where id='.$info['coach_id']);
            $info['coach']=$coach?$coach:'--';
            $file=db()->getAll('select title,file,file_type from x_user_file where uid='.$id);
            if($file) {
                $info['file']=array();
                foreach($file as $v){
                    $info['file']['img_'.$v['file_type']]=$v['file'];
                }
            }
            $this->ajaxReturn(array('status'=>1,'data'=>$info));
        }
        else{
            $this->ajaxReturn(array('status'=>0,'info'=>'没有找到该数据'));
        }
    }
    
    public function book_list(){
        $id=(int)$_GET['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'参数id错误'));
    
        $book_list=db()->getAll('select coach_id,book_kemu,book_time,status from x_coach_book where uid='.$id);
        $coach_arr=array();
        foreach($book_list as $k=> $v){
            if(isset($coach_arr[$v['id']])){
                $book_list[$k]['coach_name']=$coach_arr[$v['id']];
            }else{
                $book_list[$k]['coach_name']=$coach_arr[$v['id']]=db()->getOne('select real_name from x_user where id='.$v['coach_id'].' limit 1');
            }
        }
        if(!$book_list) $book_list=array();
    
        $this->ajaxReturn(array('status'=>1,'data'=>$book_list));
    
    }
    
    public function modify_progress(){
        $ids=$_POST['ids'];
        $kemu=(int)$_POST['kemu'];
        if(empty($ids)) $this->ajaxReturn(array('status'=>0,'info'=>'请先选择学员'));
        $data['kemu']=$kemu;
        $a=array(0=>'未报名',1=>'科目一',2=>'科目二',3=>'科目三',4=>'科目四',100=>'已毕业');
        foreach($ids as $id){
            $affert_num=db()->update('x_user',$data,'id='.$id);
            if($affert_num>0){
                insertlogs('update','批量更新学员进度为:'.$a[$kemu],$data,$id);
            }
        }
        $this->ajaxReturn(array('status'=>1));
    }
    
    //学车进度
    public function progress(){
        $id=(int)$_GET['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'参数id错误'));
        $list=db()->getAll('select * from x_jindu where uid='.$id.' order by create_time ASC');
        $this->ajaxReturn(array('status'=>1,'data'=>$list));
    }
    
    public function account_log(){
        $id=(int)$_GET['id'];
        if($id<=0) $this->ajaxReturn(array('status'=>0,'info'=>'参数id错误'));
    
        $account_list=db()->getAll('select * from x_user_amount_log where uid='.$id.' order by id desc');
    
        if(!$account_list) $account_list=array();
    
        $this->ajaxReturn(array('status'=>1,'data'=>$account_list));
    }
    
}